import {tableFetch} from '@/api/api.js'
// const tableFetch = require('@/api/api.js').tableFetch
export default {
  data () {
    return {
      tableData: [],
      tableMeta: [],
      api: '',
      pagination: {},
      currentPage: 1,
      loading: true,
      tableHeight: 450
    }
  },
  // watch: {
  //   '$route' (to, from) {
  //     // todo 暂时使用watch
  
  //     this.tableData = []
  //     this.currentPage = 1
  //     this.getInitData(to, this)
  //     this.fetchData()
  //   }
  // },
  methods: {
    showIn (showArray, not = true) {
      return showArray.indexOf(this.$route.params.type) > -1 && not
    },
    currentParam (param) {
      return this.$route.params.type === param
    },
    getNextPageData (page) {
      this.currentPage = page
      tableFetch(this)
    },
    getInitData (to, vm) {
      var params = to.params.type
      vm.tableMeta = vm.routeListData[params].tableMeta
      vm.api = vm.routeListData[params].api
      console.log(vm.api)
    },
    tableHeightQuery (array) {
      let vm = this
      let tmp = []
      let wH = +document.body.offsetHeight
      let result = true
      var total = 0
      return () => {
        array.forEach(function (item, index) {
          tmp[index] = vm.getElement(item)
          result = !!tmp[index] && result
          // console.log(result)
        })
        if (result) {
          tmp.forEach(function (item) {
            total += +vm.outerHeight(item)
          })
          vm.tableHeight = wH - total - 37
          // console.log(vm.tableHeight)
        }
      }
    },
    defaultHeight () {
      return this.tableHeightQuery(['#header', '#nav2', '#searchTable', '#footer'])()
    },
    // tableFetch直接作为组件方法 混合之后可以在组件内再次调用
    tableFetch
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
    console.group('mixins路由钩子enter',to,from)
      console.log('进入',to.fullPath)
      vm.getInitData(to, vm)
      tableFetch(vm)
      // vm.loading=false
      console.log(vm.tableData,vm.pagination,vm.loading)
      console.groupEnd()
    })
  },
  // todo https://github.com/vuejs/vue-router/issues/1218
  // mixin和组件内的beforeRouteUpdate不能共存 2017年3月16日（待作者更新
  beforeRouteUpdate (to, from, next) {
    let self = this
    this.tableData = []
    this.currentPage = 1
    this.getInitData(to, this)
    tableFetch(self)
    next(vm => {
    })
  }
  // beforeRouteLeave (to, from, next) {
  //   console.log('mixin内钩子调用成功')
  //   next(vm => {

  //   })
  // }
}
